package com.imesne.office.excel.html;

import com.imesne.office.excel.utils.ExcelKitUtils;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.File;
import java.util.Formatter;

/**
 * Created by liyd on 17/7/17.
 */
public class ExcelHtmlWriter {

    private ExcelHtmlWriterConfig excelHtmlWriterConfig;

    public ExcelHtmlWriter(ExcelHtmlWriterConfig excelHtmlWriterConfig) {
        this.excelHtmlWriterConfig = excelHtmlWriterConfig;
    }

    public ExcelHtml write(File excelFile) {
        Workbook workbook = ExcelKitUtils.createWorkbook(excelFile);
        ExcelHtmlPrinter excelHtmlPrinter = this.excelHtmlWriterConfig.getExcelHtmlPrinter();
        ExcelHtml excelHtml = excelHtmlPrinter.print(workbook);
        return excelHtml;
    }

    public ExcelHtml write(byte[] bytes) {
        Workbook workbook = ExcelKitUtils.createWorkbook(bytes);
        ExcelHtmlPrinter excelHtmlPrinter = this.excelHtmlWriterConfig.getExcelHtmlPrinter();
        ExcelHtml excelHtml = excelHtmlPrinter.print(workbook);
        return excelHtml;
    }

    public void write(byte[] bytes, Appendable output) {
        ExcelHtml excelHtml = this.write(bytes);
        Formatter out = new Formatter(output);
        out.format(excelHtml.getHtml());
        out.close();
    }

}
